﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

using Manage_Pupils.Controller;

namespace Manage_Pupils
{
    public partial class fmQuanLyDiem : Form
    {
        Connection cons= new Connection();
        DataTable dt;
        addScoreController addScoreControl = new addScoreController();
        SqlDataAdapter da = new SqlDataAdapter();
        public fmQuanLyDiem()
        {
            InitializeComponent();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void fmQuanLyDiem_Load(object sender, EventArgs e)
        {
            //addScoreControl.addFromExcel(dvManageScore);
            dt = new DataTable();
            dt = addScoreControl.getData();
            dvManageScore.DataSource = dt;
            addScoreControl.getDataScore(cbCodeSubject, cbCodePupils, cbCodeSemester, cbClass);
            this.binding();

        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            dvManageScore.DataSource = addScoreControl.getData();
            if(tbPath.Text!="") addScoreControl.addFromExcel(tbPath.Text);
            else addScoreControl.addScore(cbCodeSubject, cbCodePupils, cbCodeSemester, tbMidTerm, tbEndofPeriod, cbClass, tbMedium);
             
            dvManageScore.DataSource = addScoreControl.getData();
        }

        private void btBrowser_Click(object sender, EventArgs e)
        {
            tbPath.Text = addScoreControl.getPath();
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            //dvManageScore.DataSource = addScoreControl.getData();
          //  addScoreControl.edit(cbCodeSubject,cbCodePupils, cbCodeSemester,  tbMidTerm, tbEndofPeriod, cbClass,tbMedium,dt);

           //==========================
            cons.Connect();
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = cons.con;
            commandUpdate.CommandType = CommandType.Text;


            commandUpdate.CommandText = @"Update BangDiem Set 
                                                        MaHK=@MaHK,
                                                        DiemGK=@DiemGK,
                                                        DiemCK=@DiemCK,
                                                        Lop=@Lop,
                                                        TrungBinhHK=@TrungBinhHK
                                                Where MaMH=@MaMH and MaHS = @MaHS";
            commandUpdate.Parameters.Add("@MaMH", SqlDbType.NChar, 10).Value = cbCodeSubject.Text;
            commandUpdate.Parameters.Add("@MaHS", SqlDbType.NChar, 10).Value = cbCodePupils.Text;
           // MessageBox.Show(cbCodeSubject.Text + " " + cbCodePupils.Text);
            commandUpdate.Parameters.Add("@MaHK", SqlDbType.NChar, 10).Value = cbCodeSemester.Text;
            commandUpdate.Parameters.Add("@DiemGK", SqlDbType.Float, 5).Value = float.Parse(tbMidTerm.Text);
            commandUpdate.Parameters.Add("@DiemCK", SqlDbType.Float, 5).Value = float.Parse(tbEndofPeriod.Text);
            commandUpdate.Parameters.Add("@Lop", SqlDbType.NChar, 10).Value = cbClass.Text;
            commandUpdate.Parameters.Add("@TrungBinhHK", SqlDbType.Float, 6).Value = Math.Round(((float.Parse(tbMidTerm.Text) + float.Parse(tbEndofPeriod.Text) * 2) / 3), 2, MidpointRounding.AwayFromZero);


            if (commandUpdate.ExecuteNonQuery() > 0)
            {
               // MessageBox.Show("OK");
                dvManageScore.DataSource = null;
                dvManageScore.DataSource = addScoreControl.getData();
                return;
            }
            da.UpdateCommand = commandUpdate;
            da.Update(dt);
            //dt.Clear();
            //dt = getData();

            MessageBox.Show("Update successful");

            //==========================
            
            dvManageScore.DataSource = null; 
            dvManageScore.DataSource = addScoreControl.getData();
        }

        private void dvManageScore_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //addScoreControl.binding(cbCodeSubject, cbCodePupils, cbCodeSemester, tbMidTerm, tbEndofPeriod, cbClass, tbMedium,dt);
            //this.binding();
        }

        public void binding() {
            cbCodeSubject.DataBindings.Clear();
            cbCodeSubject.DataBindings.Add("Text", dt, "MaMH");
            cbCodePupils.DataBindings.Clear();
            cbCodePupils.DataBindings.Add("Text", dt, "MaHS");
            cbCodeSemester.DataBindings.Clear();
            cbCodeSemester.DataBindings.Add("Text", dt, "MaHK");
            tbMidTerm.DataBindings.Clear();
            tbMidTerm.DataBindings.Add("Text", dt, "DiemGK");
            tbEndofPeriod.DataBindings.Clear();
            tbEndofPeriod.DataBindings.Add("Text", dt, "DiemCK");
            cbClass.DataBindings.Clear();
            cbClass.DataBindings.Add("Text", dt, "Lop");
            tbMedium.DataBindings.Clear();
            tbMedium.DataBindings.Add("Text", dt, "TrungBinhHK");
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            cons.Connect();
            DataRow row = dt.Select("MaMH = \'" + (cbCodeSubject.Text) + "\'")[0];
            row.BeginEdit();
            row["MaHS"] = cbCodePupils.Text;
            row.Delete();
            row.EndEdit();

            SqlCommand command = new SqlCommand();
            command.Connection = cons.con;
            command.CommandType = CommandType.Text;
            
            command.CommandText = "Delete From BangDiem Where MaMH=\'"+cbCodeSubject.Text+"\' and MaHS=\'"+cbCodePupils.Text+"\'";
            command.Parameters.Add("@MaMH", SqlDbType.NChar, 10, "MaMH");
            command.Parameters.Add("@MaHS", SqlDbType.NChar, 10, "MaHS");
            da.DeleteCommand = command;
            da.Update(dt);
            MessageBox.Show("Đã xóa thành công!");
            dt = addScoreControl.getData();
            dvManageScore.DataSource = dt;
        }
       
       
    
    }
}
